//
// Colors.cs
//
// Author: 
//  Fredy Ramirez <framirez@pacificcoolwave.com>
//
// Copyright (C) 2011 Fredy Ramirez (http://www.pacificcoolwave.com)
//
//  Creation date: Oct 12, 2011
//  Last modification: 

/*
The MIT License

Copyright (C) 2011 Fredy Ramirez (http://www.pacificcoolwave.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 */


using System;
using System.Xml;
using System.Collections.Generic;
using Gtk;

namespace Pcw.Addins.Highlighting.Definition
{
	internal class Styles
	{
		private Dictionary<string, Style> styles = new Dictionary<string, Style>();
		
		internal Style this[string name] 
		{
			get {
				return styles[name];
			}
		}		
		
		internal Styles (XmlNode node)
		{
			foreach (XmlNode n in node.ChildNodes) 
			{
				XmlElement item = (XmlElement)n;
				styles.Add(XmlUtil.GetString(item, "name"), new Style(XmlUtil.GetString(item, "name"), XmlUtil.GetString(item, "foreColor"), XmlUtil.GetString(item, "backColor"),
					XmlUtil.GetBoolean(item, "bold"), XmlUtil.GetBoolean(item, "italic")));
			}
		}
	}
	
	internal class Style
	{	
		private string name;
		private string back_color;
		private string fore_color;
		private bool bold;
		private bool italic;

		internal string TagName {
			get {
				return "Pcw.Highlight.Tag." + name;
			}
		}
		
		internal Style (string styleName, string foreColor, string backColor, bool styleBold, bool styleItalic)
		{
			name = styleName;
			back_color = backColor;
			fore_color = foreColor;
			bold = styleBold;
			italic = styleItalic;
		}
		
		internal TextTag GetTextTag (int descriptorId)
		{
			TextTag tag = new TextTag(TagName +  "." + descriptorId);
			if (!String.IsNullOrEmpty(back_color))
				tag.BackgroundGdk = GetColor(back_color);
			if (!String.IsNullOrEmpty(fore_color))
				tag.ForegroundGdk = GetColor(fore_color);
			if (bold)
				tag.Weight = Pango.Weight.Bold;
			if (italic)
				tag.Style = Pango.Style.Italic;
			
			return tag;
		}
		
		private Gdk.Color GetColor(string color) 
		{
			try
			{
				string [] sColor = color.Split(',');				
				return new Gdk.Color(Convert.ToByte(sColor[0]), Convert.ToByte(sColor[1]), Convert.ToByte(sColor[2]));
			}
			catch
			{
				throw new ArgumentException("Color argument '" + color + "' in bad format. It must be rgb comma style. Ex: 255,255,255");
			}
		}
	}

}

